﻿Imports System.Windows.Input

Public Class VMTableView
    Inherits VMBase
    Implements OMANE.Common.UI.IViewModelTabSection
    Implements ICommand


    Dim _Title As String
    Dim _EXCELReferentielCRT As IReferentielCRT
    Dim _data As System.Data.DataTable
    Dim _DataXam As Infragistics.Windows.DataPresenter.XamDataGrid

    Sub New()

    End Sub
    Sub New(ByVal EXCELReferentielCRT As IReferentielCRT, ByVal InfoTableReferentiel As PTIC_Util.IInfoTableReferentiel)
        _Title = InfoTableReferentiel.NomTable
        _EXCELReferentielCRT = EXCELReferentielCRT
    End Sub


    Public Property Icone As String Implements Common.UI.IViewModelTabSection.Icone
        Get
            Return "pack://application:,,,/OMANE.Common.UI.Theme;component/Images/compass.png"
        End Get
        Set(ByVal value As String)

        End Set
    End Property

    Public Property Title As String Implements Common.UI.IViewModelTabSection.Title
        Get
            Return _Title
        End Get
        Set(ByVal value As String)

        End Set
    End Property

    Public Function CanExecute(ByVal parameter As Object) As Boolean Implements System.Windows.Input.ICommand.CanExecute
        Return True
    End Function

    Public Event CanExecuteChanged(ByVal sender As Object, ByVal e As System.EventArgs) Implements System.Windows.Input.ICommand.CanExecuteChanged

    Public Sub Execute(ByVal parameter As Object) Implements System.Windows.Input.ICommand.Execute
        _data = _EXCELReferentielCRT.SqlSelectQuery("SELECT * FROM " & _Title)

        If Not CRTModule.TestDataSet.Tables.Contains(_Title) Then
            _data.TableName = _Title
            CRTModule.TestDataSet.Tables.Add(_data)
        End If
        If CRTModule.TestDataSet.Tables.Contains("TABLE_COMPTES") And _
            CRTModule.TestDataSet.Tables.Contains("TABLE_POSTES") And _
             CRTModule.TestDataSet.Tables.Contains("TABLE_DESCCOMPTES") Then

            CRTModule.TestDataSet.Relations.Add( _
                "REL_COMPTE_DESC", _
                CRTModule.TestDataSet.Tables("TABLE_COMPTES").Columns("ID"), _
                CRTModule.TestDataSet.Tables("TABLE_DESCCOMPTES").Columns("ID_COMPTE"))

            CRTModule.TestDataSet.Relations.Add( _
                "REL_POSTE_DESC",
                CRTModule.TestDataSet.Tables("TABLE_POSTES").Columns("ID"), _
                CRTModule.TestDataSet.Tables("TABLE_DESCCOMPTES").Columns("ID_POSTE"))
        End If



        OnPropertyChanged("Data")
        'OnPropertyChanged("DataXam")
    End Sub


    Public ReadOnly Property Data As Data.DataTable
        Get
            Return _data
        End Get
    End Property
    Public ReadOnly Property DataXam As Infragistics.Windows.DataPresenter.XamDataGrid
        Get
            If _DataXam Is Nothing Then
                Dim bd = New Windows.Data.Binding("Data")
                bd.Source = Me
                _DataXam = New Infragistics.Windows.DataPresenter.XamDataGrid()

                _DataXam.FieldSettings.AllowRecordFiltering = True
                _DataXam.FieldSettings.AllowSummaries = True
                _DataXam.FieldSettings.AllowEdit = False
                _DataXam.FieldLayoutSettings.AllowAddNew = False
                _DataXam.FieldLayoutSettings.AllowDelete = False

                Windows.Data.BindingOperations.SetBinding(_DataXam, Infragistics.Windows.DataPresenter.XamDataGrid.DataSourceProperty, bd)

            End If

            Return _DataXam
        End Get
    End Property
End Class
